from tkinter import messagebox

import cv2
import numpy as np
import os #获得 dir
import math
from tkinter import *
import tkinter.filedialog
import tkinter.messagebox #这个是消息框，对话框的关键
from PIL import Image# BW 扩展 ，防止  H/W>2 ,右侧出现 轮廓检测 丢失。
import math

def eraseBPHnoise(imgBW,expectCount,Ref):# test and erase ,H h n u  N -> ||
    cv2.imshow('Img', imgBW)
    #rect=cv2.rect(0,0,10,10)
    #cv2.SetImageROI(imgBW,(0,0,10,10) )

    #// 设置要增加的颜色值
    #//add = 150;
    #// 对ROI区的各个像素的值 + add
    #//cv2.AddS(imgBW, cv2.Scalar(add), imgBW);
    #// 取消ROI区
    #//cv2.ResetImageROI(imgBW);

    #继续 二值化：把 125 170 这样的过渡的都改成255
    (h, w) = imgBW.shape
    print("h w=", h, w)
    for a in range(h):
        for b in range(w):
            #print(imgBW[a, b])  ##黑白， 过仿射变换后，竟然 不只是 0 255 ， ？？？？？？？？？？？
            if imgBW[a, b] != 0  and imgBW[a, b] != 255:
                imgBW[a, b] =255

    #分析
    my_axis=1#0
    (rows,cols)=  imgBW.shape
    sumRows = imgBW.sum(
        axis=my_axis)  # 1     #axis=0, 表示列。axis=1, 表示行。   ##here i get sum from left to right!horrizonal sum!!!
    my_axis = 0  # 0
    sumCols=imgBW.sum(
        axis=my_axis)  # 0     #axis=0, 表示列。axis=1, 表示行。   ##here i get sum from left to right!horrizonal sum!!!

    print('rows'+str(rows)+" columns "+str(cols)+"sum len "+str(len(sumCols)))
    for i in range(rows):
        print(str(i)+"=i   "+str(sumRows[i]))

    themax=max(sumCols)
    themin = min(sumCols)

    '''
    #no use 
    #导数
    for i in range(rows):
        if i != rows-1:
            b=(float)(sumRows[i+1])
            a=(float)(sumRows[i])

            delta=(b-a)
            k=delta/themax
            #if abs(k)>0.013:
                #print(str(a)+"=a "+str(b)+"=b ")
                #print("        "+str(delta)+"=delta "+str(themax)+"=max "+str(i)+"=i  k="+str((k)))
    #####end  nouse
    '''

    #Ref=1500
    '''
    print("ref ref*255:",Ref,Ref*255)
    mid=int(rows/2)  #中间开始，rev  next 两个方向
    index1=0
    index2=0
    for i in range(mid ,0,-1):
        if sumRows[i]>Ref*255:
            index1=i
            break
    for i in range(mid, rows-1):
        if sumRows[i] > Ref*255:
            index2 = i
            break
    '''

    '''
    #erase
    print("erase +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
    (h, w) = imgBW.shape
    roiImg = imgBW[index1:index2, 0:0 + w]
    (h,w)=roiImg.shape
    print("h w=",h,w)
    for a in range(h):
        for b in range(w):
            print(roiImg[a, b],"e") ##黑白， 过仿射变换后，竟然 不只是 0 255 ， ？？？？？？？？？？？
            roiImg[a,b]=0
    print("end         erase +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
    #end erase
    '''
    #erase
    (h, w) = imgBW.shape
    if h<w:#!!!!!!防止 细笔画被误擦  ！！！！！！！！！！！！！！！
        print("ref ref*255:", Ref, Ref * 255)
        for i in range(0, h):
            if sumRows[i] < Ref * 255:
                for a in range(w):
                    imgBW[i, a] = 0
    #end erase


    ###############################################
    # erase
    (h, w) = imgBW.shape
    if h > w:#!!!!!!防止 细笔画被误擦 ！！！！！！！！！！！！！！！！
        print("ref ref*255:", Ref, Ref * 255)
        for i in range(0, w):
            if sumCols[i] < Ref * 255:
                for a in range(h):
                    imgBW[a, i] = 0
    # end erase




    #cv2.waitKey(0)
    cv2.imshow('Img'+str(h)+" w="+str(w), imgBW)
    #cv2.waitKey(0)

    return imgBW,1,"erased"



if __name__ == "__main__":
    root = tkinter.Tk()
    root.withdraw()  # 隐藏
    root.update()  # 隐藏
    # 对话框，可能 不太支持 汉字。
    picfilename = tkinter.filedialog.askopenfilename(initialdir=os.getcwd())
    cv2.destroyAllWindows()
    imageRGB = cv2.imread(picfilename)
##cv2.imshow('markedImg'+y_return+str_return, markedImg)
##cv2.waitKey(0)


##messagebox.showinfo("提示", "" + y_return + "\n" + str_return)




